// 1. React核心库
import React, { useState } from 'react';

// 2. UI组件库
import { Button, Space, Popconfirm, message } from 'antd';

// 3. 图标库
import { EditOutlined, DeleteOutlined, UserOutlined, LockOutlined } from '@ant-design/icons';

// 6. 项目内部模块
import { apiClients } from '@/services/apiClient';
import { autoLoginInDev } from '@/utils/autoLogin';

// 7. 生成的类型定义
import type { UserProfile } from '@/services/generated/src';

interface ActionButtonsProps {
  record: UserProfile;
  onSuccess?: () => void;
  showEdit?: boolean;
  showDelete?: boolean;
  showToggleStatus?: boolean;
  showResetPassword?: boolean;
  onEdit?: (record: UserProfile) => void;
}

const ActionButtons: React.FC<ActionButtonsProps> = ({
  record,
  onSuccess,
  showEdit = true,
  showDelete = true,
  showToggleStatus = true,
  showResetPassword = true,
  onEdit,
}) => {
  const [deleteLoading, setDeleteLoading] = useState(false);
  const [toggleLoading, setToggleLoading] = useState(false);
  const [resetLoading, setResetLoading] = useState(false);

  // 删除用户
  const handleDelete = async () => {
    try {
      setDeleteLoading(true);
      await autoLoginInDev();
      await apiClients.users.usersDestroy({ id: record.id });
      message.success('删除成功');
      onSuccess?.();
    } catch (error) {
      message.error('删除失败');
    } finally {
      setDeleteLoading(false);
    }
  };

  // 切换用户状态
  const handleToggleStatus = async () => {
    try {
      setToggleLoading(true);
      await autoLoginInDev();
      await apiClients.users.usersToggleStatusCreate({
        id: record.id,
        userStatusRequest: { is_active: !record.is_active }
      });
      message.success(record.is_active ? '用户已禁用' : '用户已激活');
      onSuccess?.();
    } catch (error) {
      message.error('状态切换失败');
    } finally {
      setToggleLoading(false);
    }
  };

  // 重置密码
  const handleResetPassword = async () => {
    try {
      setResetLoading(true);
      await autoLoginInDev();
      await apiClients.users.usersResetPasswordCreate({
        id: record.id,
        userPasswordResetRequest: { new_password: '123456' }
      });
      message.success('密码已重置为：123456');
      onSuccess?.();
    } catch (error) {
      message.error('密码重置失败');
    } finally {
      setResetLoading(false);
    }
  };

  return (
    <Space>
      {showEdit && (
        <Button
          type="link"
          icon={<EditOutlined />}
          onClick={() => onEdit?.(record)}
        >
          编辑
        </Button>
      )}

      {showToggleStatus && (
        <Popconfirm
          title={`确定${record.is_active ? '禁用' : '激活'}该用户？`}
          onConfirm={handleToggleStatus}
        >
          <Button
            type="link"
            icon={<UserOutlined />}
            loading={toggleLoading}
          >
            {record.is_active ? '禁用' : '激活'}
          </Button>
        </Popconfirm>
      )}

      {showResetPassword && (
        <Popconfirm
          title="确定重置该用户密码？"
          description="密码将重置为：123456"
          onConfirm={handleResetPassword}
        >
          <Button
            type="link"
            icon={<LockOutlined />}
            loading={resetLoading}
          >
            重置密码
          </Button>
        </Popconfirm>
      )}

      {showDelete && (
        <Popconfirm
          title="确定删除该用户？"
          description="此操作不可恢复"
          onConfirm={handleDelete}
        >
          <Button
            type="link"
            danger
            icon={<DeleteOutlined />}
            loading={deleteLoading}
          >
            删除
          </Button>
        </Popconfirm>
      )}
    </Space>
  );
};

export default ActionButtons;
